<template>
  <base-breadcrumb :breadcrumb="!query" :title="$route.meta.title">
    <!-- 质量、安全检查和整改信息详情 -->
    <a-spin
      :spinning="loading"
      style="margin-bottom: 40px"
    >
      <a-card :bordered="false">
        <title-name title="基本信息"></title-name>
           <div style="padding-left: 30px"> 受检单位：<span v-if="projectInfo.branchCompanyName || projectInfo.projectName"> {{projectInfo.branchCompanyName || ''}}<span v-if="projectInfo.projectName">/</span>{{ projectInfo.projectName || ''}}</span>
           <span v-else>{{projectInfo.companyName || ''}}</span>
        </div>
      </a-card>
      <!-- 项目信息 -->
<!--      <base-info-->
<!--        title="项目信息"-->
<!--        is-validate-->
<!--        :form="projectInfo"-->
<!--        :edit-model="0"-->
<!--      />-->
      <!-- 检查信息 -->
      <a-card class="mb12" v-if="!combine">
        <title-name :title="checkType == 1 ? '安全检查信息' : '质量检查信息'" />
        <a-row>
          <!-- 左边表单 -->
          <a-col :span="checkType == 1 ? 24 : 8">
            <custom-form
              is-validate
              :form-list="checkType == 1 ? checkListSafe : checkListQuality[0]"
              :form="checkInfo"
              edit-model="V"
            ></custom-form>
          </a-col>
          <!-- 右边表单 -->
          <a-col v-if="checkType != 1" :span="16">
            <custom-form
                is-validate
                :form-list="checkListQuality[1]"
                :form="checkInfo"
                edit-model="V"
              ></custom-form>
          </a-col>
        </a-row>
      </a-card>
      <!-- 问题信息 -->
      <a-card
        v-if="manageType == 1"
        class="mb12"
      >
        <title-name :title="checkType == 1 ? '安全问题整改信息' : '质量问题信息'" />
        <a-row>
          <!-- 左边表单 -->
          <a-col :span="!combine ? 24 : 8">
            <custom-form
              is-validate
              :form-list="combine ? problemListCombine[0] : problemList"
              :form="problemInfo"
              edit-model="V"
            ></custom-form>
          </a-col>
          <!-- 右边表单 -->
          <a-col v-if="combine" :span="16">
            <custom-form
              is-validate
              :form-list="problemListCombine[1]"
              :form="problemInfo"
              edit-model="V"
            ></custom-form>
          </a-col>
        </a-row>
      </a-card>
      <!-- 整改结果 -->
      <a-card>
      <!-- <a-card v-if="problemRectification.length > 0"> -->
        <title-name :title="checkType == 1 ? '安全整改结果' : '质量整改信息'"></title-name>
        <!-- 整改基础信息 -->
        <custom-form
            is-validate
            :form-list="rectification"
            :form="problemInfo"
            edit-model="V"
        ></custom-form>
        <!-- 整改结果列表 -->
        <a-card v-for="(item, index) in problemRectification" :key="index" class="mb12">
          <a-row>
          <!-- 左边表单 -->
          <a-col :span="8">
            <custom-form
              ref="problemRectification"
              is-validate
              :rules="rules"
              :form-list="problemRectificationList[0]"
              :form="item"
              :edit-model="editModel == 0 ? 'V' : ''"
            >
              <!-- 整改结果 -->
              <template #rectificationResult>
                <selector
                  v-model="item.rectificationResult"
                  select-type="4"
                  :disabled="editModel == 0 || index != problemRectification.length - 1"
                  placeholder="请选择"
                />
              </template>
            </custom-form>
          </a-col>
          <!-- 右边表单 -->
          <a-col :span="16">
            <custom-form
            ref="problemRectification"
            is-validate
            :rules="rules"
            :form-list="problemRectificationList[1]"
            :form="item"
            :edit-model="editModel == 0 ? 'V' : ''"
          ></custom-form>
          </a-col>
        </a-row>

        </a-card>
      </a-card>
    </a-spin>
    <!-- 页脚操作栏 -->
    <div
      v-if="showButton"
      class="btn_box page-btn-right-top"
    >
      <a-button
        v-if="editModel != 0"
        :loading="loading"
        type="primary"
        class="button_right"
        @click="handleSubmit"
      >
        提交
      </a-button>
      <BasePrintButton
        v-if="editModel == 0"
        :before-print-fn="
          () =>
            getPritContent(
              manageType == 1
                ? ['CSCEC8XN-EQ-B30801', 'CSCEC8XN-SP-B10357'][checkType]
                : ['CSCEC8XN-SP-B10356'][checkType],
              manageType == 0 ? 'safe' : 'quality'
            )
        "
        class="button_right"
      />
      <a-button
        type="primary"
        @click="$multiTab.close()"
      >
        关闭
      </a-button>
    </div>
  </base-breadcrumb>
</template>

<script>
import Selector from '@/components/Select/Selector'
import BaseInfo from '@/components/businessComponents/BaseInfo' // 项目信息
import BasePrintButton from '@/components/BasePrintButton' // 打印按钮
import Vue from 'vue'
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置

export default {
    extends: new BaseEditPage(),
    components: {
        Selector,
        BaseInfo,
        BasePrintButton
    },
    computed: {
        // 检查信息表单列表
        checkListSafe() {
            return [
                {
                    keyName: 'checkNumber',
                    label: this.checkType == 1 ? '安全检查编号' : '质量检查编号',
                },
                {
                    keyName: 'subProject',
                    hidden: this.checkType == 1,
                    label: '分项工程'
                },
                {
                    keyName: 'checkResultName',
                    label: '检查结果'
                },
                {
                    keyName: 'checkPeople',
                    label: '检查人'
                },
                {
                    keyName: 'checkTime',
                    label: '检查时间'
                },
                {
                    keyName: 'checkTypeName',
                    label: '检查类型'
                },
                {
                    keyName: 'checkAreaName',
                    label: '检查区域',
                    span: 24
                },
                {
                    keyName: 'dutyName',
                    hidden: !this.checkInfo.dutyName,
                    label: '责任人'
                },
                {
                    keyName: 'rectificationUnit',
                    hidden: !this.checkInfo.rectificationUnit,
                    label: '整改单位'
                },
                {
                    keyName: 'fileArrays',
                    slot: true,
                    label: '现场照片',
                    span: 24
                },
                {
                    keyName: 'checkRecords',
                    hidden: !this.checkInfo.checkRecords,
                    label: '检查记录',
                    span: 24
                }
            ]
        },
        checkListQuality() {
            return [
              // 左侧表单
              [
                {
                    keyName: 'checkNumber',
                    hidden: this.manageType == 1,
                    label: this.checkType == 1 ? '安全检查编号' : '质量检查编号',
                    span: 24
                },
                {
                    keyName: 'subProject',
                    hidden: this.checkType == 1,
                    label: '分项工程',
                    span: 24
                },
                {
                    keyName: 'checkResultName',
                    hidden: this.manageType == 1,
                    label: '检查结果',
                    span: 24
                },
                {
                    keyName: 'checkTypeName',
                    hidden: this.manageType == 1,
                    label: '检查类型',
                    span: 24
                },
                {
                    keyName: 'checkAreaName',
                    label: '检查区域',
                    span: 24
                },
                {
                    keyName: 'dutyName',
                    hidden: !this.checkInfo.dutyName,
                    label: '责任人',
                    span: 24
                },
                {
                    keyName: 'rectificationUnit',
                    hidden: !this.checkInfo.rectificationUnit || this.manageType == 1,
                    label: '整改单位',
                    span: 24
                },

                {
                    keyName: 'checkRecords',
                    hidden: !this.checkInfo.checkRecords,
                    label: '检查记录',
                    span: 24
                }
              ],
              // 右侧表单
              [
                {
                    keyName: 'checkPeople',
                    label: '检查人',
                    span: 12
                },
                {
                    keyName: 'checkTime',
                    label: '检查时间',
                    span: 12
                },
                {
                    keyName: 'fileArrays',
                    type: 'uploadFile',
                    label: '现场照片',
                    span: 24,
                    businessCode: this.manageType == 1 ? 'SHE01' : 'QM01',
                    attrs: {
                      disabled: this.editModel == 0 || this.manageType == 1,
                      uploadType: 'image',
                      editModel: this.editModel,
                      multiple: true
                    }
                },
              ]
            ]
        },
        // 问题信息表单列表
        problemList() {
            return [
                {
                    keyName: 'rectificationPeople',
                    label: '整改人'
                },
                {
                    keyName: 'rectificationUnit',
                    label: '整改单位'
                },
                {
                    keyName: 'reviewer',
                    label: '复查人'
                },
                {
                    keyName: 'rectificationPeriod',
                    label: '整改期限'
                },
                // {
                //     keyName: 'problemType',
                //     hidden: this.checkType != 0,
                //     label: '问题分类'
                // },
                {
                    keyName: 'securityRisksName',
                    hidden: this.checkType != 1,
                    label: '安全隐患'
                },
                {
                    keyName: 'rectificationStatus',
                    label: '状态'
                },
                {
                    keyName: 'rectificationDescription',
                    span: 24,
                    label: '问题整改描述'
                },
                {
                    keyName: 'proDescription',
                    span: 24,
                    label: '问题描述'
                }
            ]
        },
        // 问题信息表单合并了检查信息的列表
        problemListCombine() {
            return [
               [
                  {
                    keyName: 'subProject',
                    hidden: this.checkType == 1,
                    label: '分项工程',
                    span: 24
                  },
                  {
                    keyName: 'checkAreaName',
                    label: '检查区域',
                    span: 24
                  },
                  {
                    keyName: 'dutyName',
                    hidden: !this.problemInfo.dutyName,
                    label: '责任人',
                    span: 24
                  },
                  {
                    keyName: 'proDescription',
                    span: 24,
                    label: '问题描述'
                  },
                  {
                    keyName: 'problemDescription',
                    span: 24,
                    label: '问题补充信息'
                  }
               ],
               [
                  {
                    keyName: 'checkPeople',
                    label: '检查人',
                    span: 12
                  },
                  {
                    keyName: 'checkTime',
                    label: '检查时间',
                    span: 12
                  },
                  {
                    keyName: 'fileArrays',
                    type: 'uploadFile',
                    label: '现场照片',
                    span: 24,
                    businessCode: this.manageType == 1 ? 'SHE01' : 'QM01',
                    attrs: {
                      disabled: this.editModel == 0 || this.manageType == 1,
                      uploadType: 'image',
                      editModel: this.editModel,
                      multiple: true
                    }
                  },
               ]
            ]
        },
        // 整改基础信息
        rectification() {
          return [
            {
                keyName: 'rectificationPeople',
                label: '整改人'
            },
            {
                keyName: 'rectificationUnit',
                label: '整改单位'
            },
            {
                keyName: 'rectificationPeriod',
                label: '整改期限'
            },
            {
                keyName: 'reviewer',
                label: '复查人'
            },
            {
                keyName: 'isOvertime',
                label: '是否按时整改',
                customRender(e) {
                  return e ? '否' : '是'
                }
            },
            {
                keyName: 'rectificationStatus',
                label: '状态'
            }
          ]
        },
        // 整改结果单列表
        problemRectificationList() {
            return [
                [
                  {
                      keyName: 'rectificationResult',
                      viewName: 'rectificationResultName',
                      label: '整改结果',
                      slot: this.editModel != 0,
                      span: 24,
                  },
                  {
                      keyName: 'rectificationCompletionTime',
                      type: 'a-input',
                      disabled: true,
                      label: '整改完成时间',
                      span: 24,
                  },
                  {
                      keyName: 'rectificationDescription',
                      type: 'BaseTextarea',
                      span: 24,
                      label: '整改描述',
                      disabled: this.editModel == 0,
                      span: 24,
                      attrs: {
                          rows: 6
                      }
                  },
                  {
                      keyName: 'reviewDescription',
                      label: '复查描述',
                      type: 'BaseTextarea',
                      span: 24,
                      attrs: {
                          rows: 6
                      }
                  }
                ],
                [
                  {
                    keyName: 'fileArrays',
                    type: 'uploadFile',
                    label: '整改照片',
                    span: 24,
                    businessCode: this.manageType == 1 ? 'SHE01' : 'QM01',
                    attrs: {
                      disabled: true,
                      uploadType: 'image',
                      editModel: this.editModel,
                    }
                  },
                ]
            ]
        }
    },
    data() {
      //2021-11-15备注;
      //安全检查记录详情页面，不跳这个页画了
        return {
            manageType: '0', // 质量安全管理类型：0 检查记录；1 问题整改
            checkType: '0', // 检索类型：0 质量，1 安全
            editModel: '0', // 操作类型：0 查看；1 编辑
            userInfo: {}, // 用户信息
            checkLoading: false, // 搜索loading
            checkInfo: {}, //质量检查信息
            problemInfo: {}, //质量问题信息
            projectInfo: {
              branchCompanyName: "",
              projectName: ""
            }, //项目信息
            problemRectification: [], //质量问题整改结果
            loading: false, // loading控件
            rules: {
                rectificationDescription: [
                    { required: true, whitespace: true, message: '请输入整改描述', trigger: 'change', whitespace: true }
                ],
                rectificationResult: [{ required: true, message: '请选择整改结果', trigger: 'change' }]
            }, // 校验规则
            init: false, // 检索检查编号初始化
            api: null, // 请求api
            combine: this.$route.query.combine // 是否将质量检查信息和质量问题信息合并为质量问题信息
        }
    },
    methods: {
        /**
         * 提交表单
         */
        handleSubmit() {
            // 校验整改信息
            if (this.problemRectification && this.problemRectification.length > 0) {
                for (let index in this.$refs.problemRectification) {
                    this.$refs.problemRectification[index].validate((valid) => {
                        if (valid) {
                            if (index == this.$refs.problemRectification.length - 1) {
                                let name = ['update', 'create'][this.editModel - 1]
                                this.api[name](that.form)
                                    .then((res) => {
                                        that.loading = false
                                        that.$message.success('提交成功！')
                                        that.$multiTab.close()
                                    })
                                    .catch((res) => {
                                        that.loading = false
                                        // that.$message.warning(res?.data?.message || '提交失败！')
                                    })
                            }
                        } else {
                            this.$message.warning("请填写完所有必填项信息")
                            return
                        }
                    })
                }
            }
        },
        /**
         * 初始化表单
         */
        initForm() {
            this.loading = true
            this.api
                .getDetail({
                    id: this.id
                })
                .then((res) => {
                    this.loading = false
                    console.log(res.data)
                    if(this.combine){ //质量问题跟踪将质量检查信息与质量问题信息合并，最终为质量问题信息；
                      this.problemInfo = {...res.data.checkInfo, ...res.data.problemInfo} || {}
                      this.problemInfo.rectificationDescription = res.data.problemRectification?.length>0 ? res.data.problemRectification[0].rectificationDescription : ''
                    }else {
                      this.checkInfo = res.data.checkInfo || {}
                      this.problemInfo = res.data.problemInfo || {}
                    }
                    console.log(res.data)
                    this.projectInfo = res.data.projectInfo || {}
                    this.projectInfo.projectName =  this.projectInfo.projectName || ''
                    this.problemRectification = res.data.problemRectification || []
                    if (this.editModel == 2) {
                        this.problemInfo.reviewer = this.userInfo.employee.name
                    }
                })
                .catch(() => {
                    this.loading = false
                })
        }
    },
    props:{
      // 外置参数
      query: {
        type: Object,
        default: ()=> null
      },
      // 是否显示底部按钮
      showButton: {
        type: Boolean,
        default: ()=> true
      }
    },
    mounted() {
        if(this.query && Object.keys(this.query)?.length){
          Object.assign(this, this.query)
        } else {
          //映射标题
          this.$route.meta.title =
              ['质量', '安全'][this.checkType] +
              ['检查记录', '问题跟踪'][this.manageType] +
              ['详情', '编辑', '添加'][this.editModel]
        }
        let api = require(`@/api/quality/${['quality', 'safe'][this.checkType]}`) // api
        //映射api
        this.api = api?.[['checkRecord', 'problemRectification'][this.manageType]]
        // 获取用户
        this.userInfo = Vue.getAcloudProvider().getUserInfo()
        // 初始化
        this.initForm()
    }
}
</script>

<style lang="less" scoped>
// 用户想让缩略图大些，不想点预览，说白了就是懒
::v-deep .ant-upload-list-picture-card-container,
::v-deep .ant-upload-list-picture-card .ant-upload-list-item{
  width: 500px;
  height: 500px;
}
</style>
